import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///
/// example：
///
///
///
///
///
class CupertinoPickerExt extends StatefulWidget {
  final List<Map<String, Object>> items;
  final int? defaultSelectedIndex;
  final double itemHeight;
  final double height;
  final double width;
  final String? unit;
  final Function onChanged;
  final Color backgroundColor;

  const CupertinoPickerExt({
    super.key,
    required this.items,
    required this.onChanged,
    this.unit,
    this.defaultSelectedIndex = 0,
    this.itemHeight = 37.5,
    this.backgroundColor = Colors.white,
    this.height = 150.0,
    this.width = 150.0,
  });

  @override
  State<StatefulWidget> createState() => _CupertinoPickerExtState();
}

class _CupertinoPickerExtState extends State<CupertinoPickerExt> {
  late FixedExtentScrollController scrollController;

  //设置防抖周期为300毫秒
  Duration durationTime = const Duration(milliseconds: 300);

  @override
  void initState() {
    super.initState();
    scrollController = FixedExtentScrollController(initialItem: widget.defaultSelectedIndex!);
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  // 中间分割线
  Widget _selectionOverlayWidget() {
    return Container(color: Colors.transparent);
  }

  Widget _buildColorPicker(BuildContext context) {
    return Container(
      height: widget.height,
      color: Colors.white,
      child: Stack(
        alignment: Alignment.center,
        children: [
          widget.unit != null
              ? Positioned(
                  top: widget.height / 2 - (widget.itemHeight / 2),
                  left: widget.width / 2 + 38.0,
                  child: Container(
                    alignment: Alignment.center,
                    height: widget.itemHeight,
                    child: Text(
                      widget.unit!,
                      style: TextStyle(
                        color: const Color(0xff333333),
                        fontSize: 52.sp,
                        height: 1.5,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                )
              : const Offstage(offstage: true),
          CupertinoPicker(
            magnification: 1.0,
            scrollController: scrollController,
            itemExtent: widget.itemHeight,
            useMagnifier: true,
            backgroundColor: Colors.transparent,
            onSelectedItemChanged: (int index) {
              if (mounted) {
                widget.onChanged(widget.items[index]['value']);
              }
            },
            selectionOverlay: _selectionOverlayWidget(),
            children: List.generate(widget.items.length, (int index) {
              return Container(
                alignment: Alignment.center,
                height: widget.itemHeight,
                child: Text(
                  widget.items[index]["label"].toString(),
                  style: const TextStyle(
                    color: Color(0xff333333),
                    fontSize: 21.0,
                    height: 1.2,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              );
            }),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: widget.height,
      width: widget.width,
      child: CupertinoPageScaffold(
        child: ListView(
          padding: EdgeInsets.zero,
          children: [
            _buildColorPicker(context),
          ],
        ),
      ),
    );
  }
}
